const api = require('../../utils/api.js');
const app = getApp();

let is_loading_more = false;
let is_no_more = false;

Page({
  data: {
    page: 1,
    video_list: [],
    url: '',
    hide: 'hide',
    show: false,
    animationData: {},
    classa:
      (app.tabbarindex === 0 ? false : true) ||
      app.siteInfo.tabbar_type === '2',
    useTabbar: !(app.tabbarindex === 0 && app.siteInfo.tabbar_type === '1'),
    imgUrl: app.imgUrl,
  },

  onLoad(options) {
    if (options && options.shopid) {
      app.ws_micro_shop_globalData.shopid = options.shopid;
    }
    this.setData({
      useTabbar: this.data.useTabbar && app.getTabBarStatus(),
    })
    app.pageOnLoad(this);
    this.loadMoreGoodsList();
  },

  loadMoreGoodsList() {
    let _this = this;
    if (is_loading_more) {
      return;
    }
    _this.setData({
      show_loading_bar: true,
    });
    is_loading_more = true;
    let p = _this.data.page;
    wx.showLoading({
      title: '正在加载...',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.default.video_list,
      data: {
        page: p,
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.list.length === 0) {
          is_no_more = true;
          // wx.showToast({
          //   title: '没有更多数据了',
          //   icon: 'none',
          //   duration: 2000,
          // });
        }
        let video_list = _this.data.video_list.concat(res.data.list);
        _this.setData({
          video_list,
          page: p + 1,
        });
      },
      complete: function() {
        is_loading_more = false;
        _this.setData({
          show_loading_bar: false,
        });
      },
    });
  },

  play(e) {
    let index = e.currentTarget.dataset.index;
    let video_all = wx.createVideoContext('video_' + this.data.show_video);
    video_all.pause();
    this.setData({
      show_video: index,
      show: true,
    });
    let videoContext = wx.createVideoContext('video_' + index);
    videoContext.play();
  },

  onReachBottom() {
    if (is_no_more) {
      return;
    }
    this.loadMoreGoodsList();
  },

  more(e) {
    let _this = this;
    let index = e.target.dataset.index;
    let video_list = _this.data.video_list;
    let animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
    });

    this.animation = animation;
    if (Number(video_list[index].show) !== -1) {
      animation.rotate(0).step();
      video_list[index].show = -1;
      _this.setData({
        video_list: video_list,
        animationData: this.animation.export(),
      });
    } else {
      animation.rotate(0).step();
      video_list[index].show = 0;
      _this.setData({
        video_list: video_list,
        animationData: this.animation.export(),
      });
    }
  },

  onShareAppMessage() {
    return {
      path:
        '/ws_micro_shop/pages/video/video-list?shopid=' +
        app.ws_micro_shop_globalData.shopid,
    };
  },
  onShareTimeline() {
    return {
      title: '',
      imageUrl: '',
      query: 'shopid=' +
      app.ws_micro_shop_globalData.shopid
    }
  },
});
